package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.OrganizationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.OrganizationsForm;
import com.danciyixue.boot.dcyx.model.query.OrganizationsQuery;
import com.danciyixue.boot.dcyx.model.vo.OrganizationsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 组织机构信息前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:23
 */
@Tag(name = "组织机构信息接口")
@RestController
@RequestMapping("/api/v1/organizations")
@RequiredArgsConstructor
public class OrganizationsController  {

    private final OrganizationsService organizationsService;

    @Operation(summary = "组织机构信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:organizations:query')")
    public PageResult<OrganizationsVO> getOrganizationsPage(OrganizationsQuery queryParams ) {
        IPage<OrganizationsVO> result = organizationsService.getOrganizationsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增组织机构信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:organizations:add')")
    public Result<Void> saveOrganizations(@RequestBody @Valid OrganizationsForm formData ) {
        boolean result = organizationsService.saveOrganizations(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取组织机构信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:organizations:edit')")
    public Result<OrganizationsForm> getOrganizationsForm(
        @Parameter(description = "组织机构信息ID") @PathVariable Long id
    ) {
        OrganizationsForm formData = organizationsService.getOrganizationsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改组织机构信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:organizations:edit')")
    public Result<Void> updateOrganizations(
            @Parameter(description = "组织机构信息ID") @PathVariable Long id,
            @RequestBody @Validated OrganizationsForm formData
    ) {
        boolean result = organizationsService.updateOrganizations(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除组织机构信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:organizations:delete')")
    public Result<Void> deleteOrganizationss(
        @Parameter(description = "组织机构信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = organizationsService.deleteOrganizationss(ids);
        return Result.judge(result);
    }
}
